package org.aeon.gamechatnest.dto;


import lombok.Data;
import lombok.ToString;
import org.aeon.gamechatnest.common.Constant;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * @author: MissingAeon
 * @date: 2024/5/9 15:19
 * @description: 安全用户Dto 仅用于Security框架
 */
@Data
public class SecurityUserDto implements Serializable, UserDetails {
    @Serial
    private static final long serialVersionUID = 1L;
    /**
     * 用户
     */
    private UserCacheDto userCache;
    /**
     * 帐户未过期
     */
    private Boolean isAccountNonExpired = true;
    /**
     * 帐户未锁定
     */
    private Boolean isAccountNonLocked = true;
    /**
     * 凭据是否未过期
     */
    private Boolean isCredentialsNonExpired = true;
    /**
     * 已启用
     */
    private Boolean isEnabled = true;
    /**
     * 认证信息 就是用户配置code
     */
    private List<? extends GrantedAuthority> authorities;

    @Override
    public List<? extends GrantedAuthority> getAuthorities() {
        if (Objects.isNull(authorities)) {
            authorities = userCache.getAuthority().stream().map(SimpleGrantedAuthority::new).toList();
        }
        return authorities;
    }

    @Override
    public String getUsername() {
        return userCache.getUser().getNickname() + Constant.ACCOUNT_SEPARATE_CHAR + userCache.getUser().getUserId();
    }

    @Override
    public String getPassword() {
        return userCache.getUser().getPassword();
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.isAccountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.isAccountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.isCredentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return this.isEnabled;
    }

    public String toString(){
        return this.getClass().getName();
    }
}
